using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using Access.PowerTools.Add_In.Engine;
using Access.PowerTools.Utilities;

namespace Access.PowerTools.Add_In
{
    /// <summary>
    ///   Add-in Express Add-in Module
    /// </summary>
    [GuidAttribute("48803493-F009-4656-8E38-B066E4F4394B"), ProgId("Access.PowerTools.Add-In.AddinModule")]
    public class AddinModule : AddinExpress.MSO.ADXAddinModule
    {
        public AddinModule()
        {
            InitializeComponent();
            // Please add any initialization code to the AddinInitialize 
            // event handler
        }

        private AddinExpress.MSO.ADXCommandBar adxCommandBarPowerTools;
        private AddinExpress.MSO.ADXCommandBarButton adxCommandBarButtonBackup;
        private AddinExpress.MSO.ADXCommandBarButton adxCommandBarButtonRestore;
        private AddinExpress.MSO.ADXCommandBarButton adxCommandBarButtonAbout;
        private AddinExpress.MSO.ADXRibbonTab adxRibbonTabPowerTools;
        private AddinExpress.MSO.ADXRibbonGroup adxRibbonGroup1;
        private AddinExpress.MSO.ADXRibbonButton adxRibbonButtonBackUp;
        private AddinExpress.MSO.ADXRibbonButton adxRibbonButtonRestore;
        private AddinExpress.MSO.ADXRibbonButton adxRibbonButtonAbout;
 
        #region Component Designer generated code
        /// <summary>
        /// Required by designer
        /// </summary>
        private System.ComponentModel.IContainer components;
 
        /// <summary>
        /// Required by designer support - do not modify
        /// the following method
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.adxCommandBarPowerTools = new AddinExpress.MSO.ADXCommandBar(this.components);
            this.adxCommandBarButtonBackup = new AddinExpress.MSO.ADXCommandBarButton(this.components);
            this.adxCommandBarButtonRestore = new AddinExpress.MSO.ADXCommandBarButton(this.components);
            this.adxCommandBarButtonAbout = new AddinExpress.MSO.ADXCommandBarButton(this.components);
            this.adxRibbonTabPowerTools = new AddinExpress.MSO.ADXRibbonTab(this.components);
            this.adxRibbonGroup1 = new AddinExpress.MSO.ADXRibbonGroup(this.components);
            this.adxRibbonButtonBackUp = new AddinExpress.MSO.ADXRibbonButton(this.components);
            this.adxRibbonButtonRestore = new AddinExpress.MSO.ADXRibbonButton(this.components);
            this.adxRibbonButtonAbout = new AddinExpress.MSO.ADXRibbonButton(this.components);
            // 
            // adxCommandBarPowerTools
            // 
            this.adxCommandBarPowerTools.CommandBarName = "adxCommandBarPowerTools";
            this.adxCommandBarPowerTools.CommandBarTag = "5302a2b7-2706-4d72-b037-51eac19643fe";
            this.adxCommandBarPowerTools.Controls.Add(this.adxCommandBarButtonBackup);
            this.adxCommandBarPowerTools.Controls.Add(this.adxCommandBarButtonRestore);
            this.adxCommandBarPowerTools.Controls.Add(this.adxCommandBarButtonAbout);
            this.adxCommandBarPowerTools.UpdateCounter = 2;
            // 
            // adxCommandBarButtonBackup
            // 
            this.adxCommandBarButtonBackup.Caption = "&Backup";
            this.adxCommandBarButtonBackup.ControlTag = "195f758b-074f-4060-acc3-6c09552da39f";
            this.adxCommandBarButtonBackup.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxCommandBarButtonBackup.UpdateCounter = 4;
            this.adxCommandBarButtonBackup.Click += new AddinExpress.MSO.ADXClick_EventHandler(this.adxCommandBarButtonBackup_Click);
            // 
            // adxCommandBarButtonRestore
            // 
            this.adxCommandBarButtonRestore.Caption = "&Restore";
            this.adxCommandBarButtonRestore.ControlTag = "3fff7bed-a127-42a0-a736-8d056cfd6f5c";
            this.adxCommandBarButtonRestore.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxCommandBarButtonRestore.UpdateCounter = 3;
            this.adxCommandBarButtonRestore.Click += new AddinExpress.MSO.ADXClick_EventHandler(this.adxCommandBarButtonRestore_Click);
            // 
            // adxCommandBarButtonAbout
            // 
            this.adxCommandBarButtonAbout.Caption = "&About";
            this.adxCommandBarButtonAbout.ControlTag = "000af01c-7874-4590-b4ea-1d20647a7726";
            this.adxCommandBarButtonAbout.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxCommandBarButtonAbout.UpdateCounter = 3;
            this.adxCommandBarButtonAbout.Click += new AddinExpress.MSO.ADXClick_EventHandler(this.adxCommandBarButtonAbout_Click);
            // 
            // adxRibbonTabPowerTools
            // 
            this.adxRibbonTabPowerTools.Caption = "PowerTools";
            this.adxRibbonTabPowerTools.Controls.Add(this.adxRibbonGroup1);
            this.adxRibbonTabPowerTools.Id = "adxRibbonTab_614f22b1148f4c918a10b902e0a2e399";
            this.adxRibbonTabPowerTools.Ribbons = AddinExpress.MSO.ADXRibbons.msrAccessDatabase;
            // 
            // adxRibbonGroup1
            // 
            this.adxRibbonGroup1.Caption = "Main";
            this.adxRibbonGroup1.Controls.Add(this.adxRibbonButtonBackUp);
            this.adxRibbonGroup1.Controls.Add(this.adxRibbonButtonRestore);
            this.adxRibbonGroup1.Controls.Add(this.adxRibbonButtonAbout);
            this.adxRibbonGroup1.Id = "adxRibbonGroup_c997931390be40ada3775dbaed52d42d";
            this.adxRibbonGroup1.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxRibbonGroup1.Ribbons = AddinExpress.MSO.ADXRibbons.msrAccessDatabase;
            // 
            // adxRibbonButtonBackUp
            // 
            this.adxRibbonButtonBackUp.Caption = "&BackUp";
            this.adxRibbonButtonBackUp.Id = "adxRibbonButton_c96726bdfc46425aaefe5c76c7f57f3f";
            this.adxRibbonButtonBackUp.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxRibbonButtonBackUp.Ribbons = AddinExpress.MSO.ADXRibbons.msrAccessDatabase;
            this.adxRibbonButtonBackUp.OnClick += new AddinExpress.MSO.ADXRibbonOnAction_EventHandler(this.adxRibbonButtonBackUp_OnClick);
            // 
            // adxRibbonButtonRestore
            // 
            this.adxRibbonButtonRestore.Caption = "&Restore";
            this.adxRibbonButtonRestore.Id = "adxRibbonButton_d59626bb40714a12a18b1cb44c585614";
            this.adxRibbonButtonRestore.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxRibbonButtonRestore.Ribbons = AddinExpress.MSO.ADXRibbons.msrAccessDatabase;
            this.adxRibbonButtonRestore.OnClick += new AddinExpress.MSO.ADXRibbonOnAction_EventHandler(this.adxRibbonButtonRestore_OnClick);
            // 
            // adxRibbonButtonAbout
            // 
            this.adxRibbonButtonAbout.Caption = "&About";
            this.adxRibbonButtonAbout.Id = "adxRibbonButton_87896cc5ae8a4b99a8bbab85aa5d7177";
            this.adxRibbonButtonAbout.ImageTransparentColor = System.Drawing.Color.Transparent;
            this.adxRibbonButtonAbout.Ribbons = AddinExpress.MSO.ADXRibbons.msrAccessDatabase;
            this.adxRibbonButtonAbout.OnClick += new AddinExpress.MSO.ADXRibbonOnAction_EventHandler(this.adxRibbonButtonAbout_OnClick);
            // 
            // AddinModule
            // 
            this.AddinName = "Access.PowerTools.Add-In";
            this.SupportedApps = AddinExpress.MSO.ADXOfficeHostApp.ohaAccess;

        }
        #endregion
 
        #region Add-in Express automatic code
 
        // Required by Add-in Express - do not modify
        // the methods within this region
 
        public override System.ComponentModel.IContainer GetContainer()
        {
            if (components == null)
                components = new System.ComponentModel.Container();
            return components;
        }
 
        [ComRegisterFunctionAttribute]
        public static void AddinRegister(Type t)
        {
            AddinExpress.MSO.ADXAddinModule.ADXRegister(t);
        }
 
        [ComUnregisterFunctionAttribute]
        public static void AddinUnregister(Type t)
        {
            AddinExpress.MSO.ADXAddinModule.ADXUnregister(t);
        }
 
        public override void UninstallControls()
        {
            myAddDestroy();
            base.UninstallControls();
        }

        #endregion

        public Access._Application AccessApp
        {
            get
            {
                return (HostApplication as Access._Application);
            }
        }

        private bool _initialized;
        private void myAddInInitialize()
        {
            if (_initialized) return;

            Access.PowerTools.Settings.Globals.Init();

            AddInCommunicationBridge bridge =
                new AddInCommunicationBridge();
            bridge.SetHostApplication(AccessApp);   
            Access.PowerTools.Settings.Globals.HostApplication =
                bridge;

            _initialized = true;
        }

        private bool _destoryed;
        private void myAddDestroy()
        {
            if (_destoryed) return;

            AddInCommunicationBridge bridge =
                Access.PowerTools.Settings.Globals.HostApplication
                 as AddInCommunicationBridge;
            if (bridge != null)
            {
                bridge.SetHostApplication(null);
                Access.PowerTools.Settings.Globals.HostApplication = null;
            }

            _destoryed = true;

            GC.Collect(); 
        }

        private void adxCommandBarButtonBackup_Click(object sender)
        {
            myAddInInitialize();
            backUp();
        }


        private void adxRibbonButtonBackUp_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
        {
            myAddInInitialize();
            backUp();
        }

        private void backUp()
        {
            try
            {
                CommandsProcessor.BackUp();
            }
            catch (Exception ex)
            {
                Dialogs.ShowErrorMessage("BackUp: " + ex.Message);
            }
        }


        private void adxCommandBarButtonRestore_Click(object sender)
        {
            myAddInInitialize();
            restore();
        }
        private void adxRibbonButtonRestore_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
        {
            myAddInInitialize();
            restore();
        }

        private void restore()
        {
            try
            {
                CommandsProcessor.Restore();
            }
            catch (Exception ex)
            {
                Dialogs.ShowErrorMessage("Restore: " + ex.Message);
            }
        }

        private void adxCommandBarButtonAbout_Click(object sender)
        {
            myAddInInitialize();
            about();   
        }

        private void adxRibbonButtonAbout_OnClick(object sender, AddinExpress.MSO.IRibbonControl control, bool pressed)
        {
            myAddInInitialize();
            about();   
        }

        private void about()
        {
            try
            {
                CommandsProcessor.About();
            }
            catch (Exception ex)
            {
                Dialogs.ShowErrorMessage("About: " + ex.Message);
            }
        }

    }
}

